package com.kang.service.play2.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kang.bean.bot.play2.Lv;
import com.kang.bean.bot.play2.Role;
import com.kang.bean.bot.play2.Speed;
import com.kang.bean.bot.play2.vo.RoleVo;
import com.kang.mapper.play2.RoleMapper;
import com.kang.service.play2.LvService;
import com.kang.service.play2.RoleService;
import com.kang.service.play2.SpeedService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author K.faWu
 * @program service
 * @description:
 * @create 2022-08-19 17:10
 **/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    private final SpeedService speedService;
    @Autowired
    private LvService lvService;

    static Map<Long, Speed> speedMap = new HashMap<>();

    static Map<String, Lv> lvMap = new HashMap<>();

    public static Map<Long, Speed> getSpeedMap(){
        return speedMap;
    }

    public static Map<String, Lv> getLvMap(){
        return lvMap;
    }

    public RoleServiceImpl(SpeedService speedService, LvService lvService) {
        this.speedService = speedService;
        this.lvService = lvService;

        List<Speed> speedList = this.speedService.getAll();
        for (Speed speed : speedList) {
            speedMap.put(speed.getId(), speed);
        }

        List<Lv> lvList = this.lvService.getAll();
        for (Lv lv: lvList) {
            lvMap.put(lv.getLv() + lv.getType(), lv);
        }
    }


    @Override
    public Role init(String accountCode, String name, String sex) {
        Role role = new Role(accountCode, name, sex);
        int gasNum = speedService.getGasNum();
        role.setGasNum(gasNum);
        roleMapper.insert(role);
        return role;
    }

    @Override
    public RoleVo cultivation(Role role) {

        //获取用户信息，根据等级以及先天之气数量进行获取经验
        Speed speed = speedMap.get(role.getGasNum().longValue());
        Lv lv = lvMap.get(role.getLv() + role.getLvType());
        BigDecimal speed1 = speed.getSpeed();
        BigDecimal exp = lv.getExp();

        //计算出本次要增加的经验
        BigDecimal addExp = speed1.multiply(exp);
        //判断该次增加的经验是否超出了当前等级需要的经验
        if (lv.getExpMax().compareTo(role.getExp().add(addExp)) <= 0) {
            //等级经验超过了
            role.setExp(lv.getExpMax());
        } else {
            //未升级
            role.setExp(role.getExp().add(addExp));
        }
        roleMapper.updateById(role);

        RoleVo roleVo = new RoleVo();
        BeanUtils.copyProperties(role, roleVo);
        roleVo.setExpMax(lv.getExpMax());
        roleVo.setLvName(lv.getName());
        return roleVo;
    }
}
